import Router from '../../../utils/router';
import Record_server from '../../../services/record-stat';
import Record from '../../../utils/page-record.js';
import base from '../../../config/base.js';
import service from '../../../services/collage';
import ext from '../../../utils/login';
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    goodsList: {
      type: Object,
      value: {}
    },
    user_info: {
      type: Object,
      value: {}
    },
    store_id: {
      type: String
    },
    pageName: {
      type: String
    },
    scrolltopnum: {
      type: String,
      value: '',
      observer: function (newVal, oldVal, changedPath) {
        this.queryMultipleNodes(newVal)
      }
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    icon_play: base.static + 'store/play2.png',    
    icon_mask_sellout: base.static + 'collage/out.png',
    goodsList: '',
    store_id: '',
    user_info: '',
    pageName: '',
    noAuth: false,
  },
  ready() {
    let that = this
    let properties = that.properties;
    that.setData({
      goodsList: properties.goodsList,
      store_id: properties.store_id,
      pageName: properties.pageName,
      user_info: properties.user_info,
    });
  },
  /**
   * 组件的方法列表
   */
  methods: {
    //授权
    onGetUserInfo: function (e) {
      let that = this;
      let d = e.detail;
      if (d.errMsg == 'getUserInfo:ok') {
        ext.getUserInfo(d, b => {
          if (b) {
            that.setData({
              noAuth: false,
            })
            wx.showToast({
              title: '请重新点击免费拿',
              icon: 'none'
            })
          }
        });
      }
    },
    queryMultipleNodes(scroll) {
      let that = this;
      let query = wx.createSelectorQuery().in(that);
      let scrolls = 0;
      let index = 0;
      let s = wx.getSystemInfoSync().windowHeight;
      query.selectAll('.test').boundingClientRect(function (rects) {
        if (scrolls < scroll) {
          for (let i = index, len = rects.length; i < len; i++) {
            let rect = rects[i];
            if (rect && 0 < rect.top && rect.bottom < s) {
              that.viewRecord(rect.id, "view")
              index = i;
              scrolls = scroll
            }
          }
        } else {
          for (let i = index; i >= 0; i--) {
            let rect = rects[i];
            if (rect && 0 < rect.top && rect.bottom < s) {
              index = i;
              scrolls = scroll
            }
          }
        }
      }).exec()
    },
    //滚动和点击屏幕记录
    viewRecord: function (goodsid, act) {
      let that = this;
      let params = {
        name: "ltxx_buyer",
        page: that.data.pageName,
        position: '',
        productid: goodsid,
        userid: that.data.user_info.id,
        storeid: that.data.store_id,
        act: act,
        env: getApp().globalData.env
      }
      Record_server.recordstat(params, function () { });
    },
    // 发起拼团订单
    createOrder: function(e){
      let that = this;
      let inx = e.currentTarget.dataset.inx;
      let goods_id = e.currentTarget.dataset.goodsid;
      let collagegoodsid = e.currentTarget.dataset.collagegoodsid
      let name = e.currentTarget.dataset.name;
      let goods_pic = e.currentTarget.dataset.goodspic;
      let sale_price = e.currentTarget.dataset.money;
      if (that.data.user_info.info_authorize != 1){
        that.setData({
          noAuth: true,
        })
        return;
      }
      let params = {
        cegsid: collagegoodsid,
        goods_pic: goods_pic,
        goods_title: name,
        bslnid: that.data.store_id,
        goods_list_money: sale_price
      }
      service.initiatecollageorder(params, function (res) {
        if (res.code && res.code == 1) {
          Router.go("free", `store_id=${that.data.store_id}&ceorid=${res.data.ceorid}`);
          // 更改父级列表ceorid
          let myEventDetail = {
            inx: inx,
            ceorid: res.data.ceorid,
          }
          that.triggerEvent('myevent', myEventDetail)
        } else if (res.code && res.code == 102){
          wx.showToast({
            title: res.msg,
            duration: 1500,
            icon: 'none'
          })
        } else if (res.code == 0) {
          wx.showToast({
            title: '该商品已经被抢光啦',
            duration: 1500,
            icon: 'none'
          })
        }
      });
      that.viewRecord(goods_id, "click");
    },
    //继续邀请
    goFree: function(e){
      let that = this;
      let ceorid = e.currentTarget.dataset.ceorid;
      Router.go("free", `store_id=${that.data.store_id}&ceorid=${ceorid}`);
    },
    // 注册按钮记录
    btnClick: function () {
      let p = getCurrentPages().pop();
      Record.clickButton(p, 'free_authorization')
    },
  }
})